delegate 0.4.3

Method delegation with less boilerplate
Documentation

This crate removes some boilerplate for structs that simply delegate some of their methods to one or more of their fields.

It gives you the delegate! macro, which delegates method calls to selected expressions (usually inner fields).

Features:

  • Delegate to a method with a different name
use delegate::delegate;

struct Stack { inner: Vec<u32> }
impl Stack {
delegate! {
to self.inner {
#[call(push)]
pub fn add(&mut self, value: u32);
}
}
}
  • Use an arbitrary inner field expression
use delegate::delegate;

use std::rc::Rc;
use std::cell::RefCell;
use std::ops::Deref;

struct Wrapper { inner: Rc<RefCell<Vec<u32>>> }
impl Wrapper {
delegate! {
to self.inner.deref().borrow_mut() {
pub fn push(&mut self, val: u32);
}
}
}
  • Change the return type of the delegated method using a From impl or omit it altogether
use delegate::delegate;

struct Inner;
impl Inner {
pub fn method(&self, num: u32) -> u32 { num }
}
struct Wrapper { inner: Inner }
impl Wrapper {
delegate! {
to self.inner {
// calls method, converts result to u64
#[into]
pub fn method(&self, num: u32) -> u64;

// calls method, returns ()
#[call(method)]
pub fn method_noreturn(&self, num: u32);
}
}
}
  • Delegate to multiple fields
use delegate::delegate;

struct MultiStack {
left: Vec<u32>,
right: Vec<u32>,
}
impl MultiStack {
delegate! {
to self.left {
// Push an item to the top of the left stack
#[call(push)]
pub fn push_left(&mut self, value: u32);
}
to self.right {
// Push an item to the top of the right stack
#[call(push)]
pub fn push_right(&mut self, value: u32);
}
}
}
  • Delegation of generic methods
  • Inserts #[inline(always)] automatically (unless you specify #[inline] manually on the method)